Skip to main content
OpenCLIP supports creating custom model architectures through JSON configuration files and flexible model building APIs. You can define custom vision encoders, text encoders, or use pre-trained models from HuggingFace as text encoders.

Model Configuration Files

Model architectures are defined in JSON configuration files located in src/open_clip/model_configs/. Each config file specifies the model’s architecture parameters.

Basic Model Config Structure

Key Parameters

  • embed_dim: The dimension of the joint embedding space where image and text features are projected
  • vision_cfg: Configuration for the vision encoder
    • image_size: Input image resolution
    • layers: Number of transformer layers
    • width: Hidden dimension size
    • patch_size: Size of image patches for Vision Transformer
  • text_cfg: Configuration for the text encoder
    • context_length: Maximum text sequence length
    • vocab_size: Size of the vocabulary
    • width: Hidden dimension size
    • heads: Number of attention heads
    • layers: Number of transformer layers

Adding Custom Model Configs

You can add your own model configurations using the add_model_config() function:

Using HuggingFace Models as Text Encoders

OpenCLIP allows you to use any HuggingFace transformer model as the text encoder. This is useful for leveraging pre-trained language models or multilingual models.

HuggingFace Text Encoder Config

Training with HuggingFace Text Encoder

When training with a HuggingFace model as the text encoder, use the --hf-tokenizer-name parameter to specify the tokenizer:

Freezing and Unfreezing Layers

You can control which layers of the text encoder are trainable:
Parameters:
  • --lock-text: Freeze the entire text encoder
  • --lock-text-unlocked-layers N: Leave the last N layer groups unfrozen for fine-tuning
  • --lock-text-freeze-layer-norm: Freeze LayerNorm running stats in locked layers

Custom Vision Architectures

OpenCLIP supports various vision encoder architectures:

Vision Transformer (ViT)

Standard Vision Transformer configuration:

ConvNeXt

Using timm models for vision encoding:

Creating Models Programmatically

You can also create custom models directly in Python:

Available Model Configs

To see all available model configurations:

Best Practices

  1. Embed Dimension: Ensure embed_dim is consistent across vision and text towers
  2. Model Naming: Use descriptive names that indicate architecture (e.g., roberta-ViT-B-32)
  3. Configuration Testing: Test custom configs with small datasets before full training
  4. Pre-trained Weights: When using HuggingFace models, leverage their pre-trained weights for better initialization
  5. Layer Freezing: Start with more frozen layers and gradually unfreeze for fine-tuning

Example: Training Custom Model

Complete example training a custom model with RoBERTa text encoder:
This configuration:
  • Uses RoBERTa as the text encoder
  • Keeps the first layers of RoBERTa frozen, unfreezing the last 10 layers
  • Trains on data from S3
  • Uses automatic mixed precision for efficiency
  • Reports metrics to TensorBoard